package com.yutian.selfservice.support.upload.config;


import com.yutian.selfservice.support.upload.handler.Upload;
import com.yutian.selfservice.support.upload.handler.storage.Storage;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;

import java.util.Map;


@Configuration
@ConditionalOnBean(LocalResourceConfig.class)
@EnableConfigurationProperties({UploadConfig.UploadProperties.class})
public class UploadConfig {
    @Autowired
    private Map<String, Storage> storageMap;

    @Bean()
    public Upload upload(UploadProperties uploadProperties) {
        Storage storage = storageMap.get(uploadProperties.getStorageType());
        Assert.notNull(storage, "存储类型" + uploadProperties.getStorageType() + "没有对应的实现");
        return new Upload(storage, uploadProperties);
    }

    @ConfigurationProperties(
            prefix = "document.upload"
    )
    @Data
    public static class UploadProperties {
        /**
         * 路径根路径
         */
        private String rootPath;
        /**
         * 存储类型
         */
        private String storageType;
        /**
         * 本地访问url
         */
        private String accessUrl;
    }

}
